package com.dawn.module.hospital.service.ehealth;

import com.alibaba.fastjson.JSON;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.hospital.service.ehealth.api.EHealthTengmedApi;
import com.dawn.module.hospital.service.ehealth.bean.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author cnsu-cmh
 * @date 2024-08-24 11:00
 * @description
 */
@Slf4j
@Service
public class HealthCardService {

    protected static final Integer ERROR_CODE = ErrorCodeConstants.BH_ERROR.getCode();

    /***
     * 功能描述: 根据身份证照片提取身份证信息
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:48
     * @param ocrReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult ocrInfo(OcrReq ocrReq){
        log.info("eHealth tengmed ocrInfo:"+ JSON.toJSONString(ocrReq));
        if(StringUtils.isEmpty(ocrReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE,"医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(ocrReq.getImageContent())) {
            return CommonResult.error(ERROR_CODE, "图片base64内容不能为空-imageContent");
        }

        CommonResult validated = baseValidated(ocrReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.ocrInfo(ocrReq);
        return commonResult;
    }

    /***
     * 功能描述: 注册健康卡
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:49
     * @param regReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult register(RegReq regReq) {
        log.info("eHealth tengmed register:"+ JSON.toJSONString(regReq));
        if(StringUtils.isEmpty(regReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }
        if(StringUtils.isEmpty(regReq.getWechatCode())) {
            return CommonResult.error(ERROR_CODE, "微信身份码不能为空-wechatCode");
        }
        if(StringUtils.isEmpty(regReq.getName())) {
            return CommonResult.error(ERROR_CODE, "姓名不能为空-name");
        }
        if(StringUtils.isEmpty(regReq.getGender())) {
            return CommonResult.error(ERROR_CODE, "性别不能为空-gender");
        }
        if(StringUtils.isEmpty(regReq.getNation())) {
            regReq.setNation("未知");
//            return CommonResult.error(ERROR_CODE, "民族不能为空-nation");
        }
        if(StringUtils.isEmpty(regReq.getBirthday())) {
            return CommonResult.error(ERROR_CODE, "出生年月日不能为空-birthday");
        }
        if(StringUtils.isEmpty(regReq.getIdType())) {
            return CommonResult.error(ERROR_CODE, "证件类型不能为空-idType");
        }
        if(StringUtils.isEmpty(regReq.getPhone1())) {
            return CommonResult.error(ERROR_CODE, "联系方式1不能为空-phone1");
        }
        CommonResult validated = baseValidated(regReq);
        if(validated.isError()) {
            return validated;
        }
        CommonResult commonResult = EHealthTengmedApi.register(regReq);
        return commonResult;
    }

    /**
     * 通过健康卡授权码获取健康卡
     * @param healthCodeReq
     * @return
     */
    public CommonResult getHealthCardByHealthCode(HealthCodeReq healthCodeReq){
        log.info("eHealth tengmed getByHealthCode:"+ JSON.toJSONString(healthCodeReq));
        if(StringUtils.isEmpty(healthCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(healthCodeReq.getHealthCode())) {
            return CommonResult.error(ERROR_CODE, "健康卡授权码不能为空-healthCode");
        }

        CommonResult validated = baseValidated(healthCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.getByHealthCode(healthCodeReq);
        return commonResult;
    }

    /***
     * 功能描述: 通过健康卡二维码获取健康卡
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:49
     * @param healthCodeReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult getHealthCardByQRCode(HealthCodeReq healthCodeReq){
        log.info("eHealth tengmed getByQRCode:"+ JSON.toJSONString(healthCodeReq));
        if(StringUtils.isEmpty(healthCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(healthCodeReq.getQrCodeText())) {
            return CommonResult.error(ERROR_CODE, "健康卡二维码不能为空-qrCodeText");
        }

        CommonResult validated = baseValidated(healthCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.getByQRCode(healthCodeReq);
        return commonResult;
    }

    /**
     * 获取健康卡二维码
     * @param qrCodeReq
     * @return
     */
    public CommonResult getQRCode(QRCodeReq qrCodeReq){
        log.info("eHealth tengmed getQRCode:"+ JSON.toJSONString(qrCodeReq));
        if(StringUtils.isEmpty(qrCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getHealthCardId())) {
            return CommonResult.error(ERROR_CODE, "健康卡ID不能为空-healthCardId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getIdType())) {
            qrCodeReq.setIdType("01");
        }
        if(StringUtils.isEmpty(qrCodeReq.getCodeType())) {
            qrCodeReq.setCodeType("0");
        }

        CommonResult validated = baseValidated(qrCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.getDynamicQRCode(qrCodeReq);
        return commonResult;
    }

    /***
     * 功能描述: 电子健康卡用卡监测数据上传
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:49
     * @param reportDataReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult uploadData(ReportDataReq reportDataReq){
        log.info("eHealth tengmed uploadData:"+ JSON.toJSONString(reportDataReq));
        if(StringUtils.isEmpty(reportDataReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }
        if(StringUtils.isEmpty(reportDataReq.getQrCodeText())) {
            return CommonResult.error(ERROR_CODE, "二维码文本不能为空-qrCodeText");
        }
        if(StringUtils.isEmpty(reportDataReq.getScene())) {
            return CommonResult.error(ERROR_CODE, "用卡环节不能为空-scene");
        }

        if((reportDataReq.getScene().equals("010101") ||
                reportDataReq.getScene().equals("0101011") ||
                reportDataReq.getScene().equals("0101012") ||
                reportDataReq.getScene().equals("0201016") ) &&
                StringUtils.isEmpty(reportDataReq.getDepartment())
        ) {
            return CommonResult.error(ERROR_CODE, "用卡科室不能为空-department");
        }
        if(StringUtils.isEmpty(reportDataReq.getCardType())) {
            reportDataReq.setCardType("11");
        }
        if(StringUtils.isEmpty(reportDataReq.getCardChannel())) {
            reportDataReq.setCardChannel("0401");
        }
        if(StringUtils.isEmpty(reportDataReq.getCardCostTypes())) {
            reportDataReq.setCardCostTypes("0100");
        }

        CommonResult validated = baseValidated(reportDataReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.reportHISData(reportDataReq);

        return commonResult;
    }

    /***
     * 功能描述: 获取卡包订单ID
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:50
     * @param orderIdReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult getOrderIdByOutAppId(HealthCodeReq orderIdReq){
        log.info("eHealth tengmed getOrderId:"+ JSON.toJSONString(orderIdReq));
        if(StringUtils.isEmpty(orderIdReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(orderIdReq.getQrCodeText())) {
            return CommonResult.error(ERROR_CODE, "健康卡二维码不能为空-qrCodeText");
        }

        CommonResult validated = baseValidated(orderIdReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.getOrderIdByOutAppId(orderIdReq);
        return commonResult;
    }

    /***
     * 功能描述: 绑定健康卡和院内ID关系
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:53
     * @param bindCardReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult bindCardRelation(BindCardReq bindCardReq){
        log.info("eHealth tengmed bindCardRelation:"+ JSON.toJSONString(bindCardReq));
        if(org.springframework.util.StringUtils.isEmpty(bindCardReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE,"医院ID不能为空-hospitalId");
        }

        if(org.springframework.util.StringUtils.isEmpty(bindCardReq.getQrCodeText())) {
            return CommonResult.error(ERROR_CODE,"健康卡二维码不能为空-qrCodeText");
        }


        CommonResult validated = baseValidated(bindCardReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.bindCardRelation(bindCardReq);
        return commonResult;
    }

    /***
     * 功能描述: 批量注册电子健康卡
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:50
     * @param regReqs:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult registerBatch(List<RegReq> regReqs) {
        log.info("eHealth tengmed registerBatch:"+ JSON.toJSONString(regReqs));
        if(CollectionUtils.isEmpty(regReqs))
            return CommonResult.error(ERROR_CODE, "健康卡信息不能为空");
        if(regReqs.size() > 15)
            return CommonResult.error(ERROR_CODE, "健康卡信息最多不能超过15条");

        RegReq req = regReqs.get(0);
        if(StringUtils.isEmpty(req.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        CommonResult validated = baseValidated(req);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.registerBatch(regReqs);
        return commonResult;
    }

    /***
     * 功能描述: openId加密
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:54
     * @param openIdReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult activeCardOpenid(ActiveCardOpenidReq openIdReq) {
        log.info("eHealth tengmed activeCardOpenid:"+ JSON.toJSONString(openIdReq));
        if(StringUtils.isEmpty(openIdReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(openIdReq.getOpenId())) {
            return CommonResult.error(ERROR_CODE, "openId不能为空-openId");
        }

        CommonResult validated = baseValidated(openIdReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.activeCardOpenid(openIdReq);
        return commonResult;
    }

    /***
     * 功能描述: 自助机创建二维码
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:55
     * @param qrCodeReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult ssmGenQrCode(SsmGenQrCodeReq qrCodeReq) {
        log.info("eHealth tengmed ssmGenQrCode:"+ JSON.toJSONString(qrCodeReq));
        if(StringUtils.isEmpty(qrCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getSsmId())) {
            return CommonResult.error(ERROR_CODE, "自助机编号不能为空-ssmId");
        }

        CommonResult validated = baseValidated(qrCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.ssmGenQrCode(qrCodeReq);
        return commonResult;
    }

    /***
     * 功能描述: 自助机获取用户扫码授权结果
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:56
     * @param qrCodeReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult ssmQueryQrCodeResult(SsmQueryQrCodeReq qrCodeReq) {
        log.info("eHealth tengmed ssmQueryQrCodeResult:"+ JSON.toJSONString(qrCodeReq));
        if(StringUtils.isEmpty(qrCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getUid())) {
            return CommonResult.error(ERROR_CODE, "二维码uid不能为空-uid");
        }

        CommonResult validated = baseValidated(qrCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.ssmQueryQrCodeResult(qrCodeReq);
        return commonResult;
    }

    /***
     * 功能描述: 自助机通过健康卡ID获取健康卡信息
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:57
     * @param qrCodeReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult getHealthCardByHealthCardId(QRCodeReq qrCodeReq) {
        log.info("eHealth tengmed ssmGenQrCode:"+ JSON.toJSONString(qrCodeReq));
        if(StringUtils.isEmpty(qrCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getHealthCardId())) {
            return CommonResult.error(ERROR_CODE, "健康卡ID不能为空-healthCardId");
        }

        CommonResult validated = baseValidated(qrCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.getHealthCardByHealthCardId(qrCodeReq);
        return commonResult;
    }

    /***
     * 功能描述: 自助机获取用户手机号生成二维码
     * @auther: cnsu-cmh
     * @date: 2024-08-24 11:59
     * @param qrCodeReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult machineGenQrCode(SsmGenQrCodeReq qrCodeReq) {
        log.info("eHealth tengmed machineGenQrCode:"+ JSON.toJSONString(qrCodeReq));
        if(StringUtils.isEmpty(qrCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getSsmId())) {
            return CommonResult.error(ERROR_CODE, "自助机编号不能为空-ssmId");
        }

        CommonResult validated = baseValidated(qrCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.machineGenQrCode(qrCodeReq);
        return commonResult;
    }

    /***
     * 功能描述: 自助机获取用户手机号授权结果
     * @auther: cnsu-cmh
     * @date: 2024-08-24 12:05
     * @param qrCodeReq:
     * @return: com.dawn.framework.common.pojo.CommonResult
     */
    public CommonResult machineQueryQrCodeResult(SsmQueryQrCodeReq qrCodeReq) {
        log.info("eHealth tengmed machineQueryQrCodeResult:"+ JSON.toJSONString(qrCodeReq));
        if(StringUtils.isEmpty(qrCodeReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(qrCodeReq.getUid())) {
            return CommonResult.error(ERROR_CODE, "二维码uid不能为空-uid");
        }

        CommonResult validated = baseValidated(qrCodeReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.machineQueryQrCodeResult(qrCodeReq);
        return commonResult;
    }

    /**
     * @description: 实人认证生成orderId接口
     * @param verifyOrderReq
     * @return: com.dawn.framework.common.pojo.CommonResult
     * @author: cnsu-cmh
     * @time: 2024/11/11 17:10
     */
    public CommonResult registerUniformVerifyOrder(RegisterUniformVerifyOrderReq verifyOrderReq) {
        log.info("eHealth tengmed registerUniformVerifyOrder:"+ JSON.toJSONString(verifyOrderReq));
        if(StringUtils.isEmpty(verifyOrderReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(verifyOrderReq.getIdCard())) {
            return CommonResult.error(ERROR_CODE, "证件号码不能为空-idCard");
        }

        if(StringUtils.isEmpty(verifyOrderReq.getCardType())) {
            verifyOrderReq.setCardType("01");
        }

        CommonResult validated = baseValidated(verifyOrderReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.registerUniformVerifyOrder(verifyOrderReq);
        return commonResult;
    }

    /**
     * @description: 实人认证结果查询接口
     * @param checkUniformVerifyReq
     * @return: com.dawn.framework.common.pojo.CommonResult
     * @author: cnsu-cmh
     * @time: 2024/11/11 17:35
     */
    public CommonResult checkUniformVerifyResult(CheckUniformVerifyReq checkUniformVerifyReq) {
        log.info("eHealth tengmed checkUniformVerifyResult:"+ JSON.toJSONString(checkUniformVerifyReq));
        if(StringUtils.isEmpty(checkUniformVerifyReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }

        if(StringUtils.isEmpty(checkUniformVerifyReq.getVerifyOrderId())) {
            return CommonResult.error(ERROR_CODE, "认证订单ID不能为空-verifyOrderId");
        }


        if(StringUtils.isEmpty(checkUniformVerifyReq.getRegisterOrderId())) {
            return CommonResult.error(ERROR_CODE, "认证结果数据不能为空-registerOrderId");
        }


        CommonResult validated = baseValidated(checkUniformVerifyReq);
        if(validated.isError()) {
            return validated;
        }

        CommonResult commonResult = EHealthTengmedApi.checkUniformVerifyResult(checkUniformVerifyReq);
        return commonResult;
    }

    private CommonResult baseValidated(BaseReq baseReq) {
        if(StringUtils.isEmpty(baseReq.getAppId())) {
            return CommonResult.error(ERROR_CODE, "开发者ID不能为空-appId");
        }
        if(StringUtils.isEmpty(baseReq.getAppSecret())) {
            return CommonResult.error(ERROR_CODE, "开发者密码不能为空-appSecret");
        }
        if(StringUtils.isEmpty(baseReq.getHospitalId())) {
            return CommonResult.error(ERROR_CODE, "医院ID不能为空-hospitalId");
        }
        return CommonResult.success(true);
    }
}
